﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using Dapper.Logic.Implement;
using Dapper.Logic.Service;
using System.Reflection;

namespace Dapper.Logic
{
    public class LogicService
    {
        private static IContainer container;
        static LogicService()
        {
            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterType<UserLogicImpl>().As<IUserLogicService>();


            var assembly = Assembly.GetExecutingAssembly();
            builder.RegisterAssemblyTypes(assembly).Where(x => x.Name.EndsWith("LogicImpl")).AsImplementedInterfaces().SingleInstance();
            container = builder.Build();
        }

        public static IUserLogicService IUserLogicService
        {
            get
            {
                return container.Resolve<IUserLogicService>();
            }
        }

        public static IActionLogicService IActionLogicService
        {
            get
            {
                return container.Resolve<IActionLogicService>();
            }
        }
    }

    public class LogicService<T>
    {
        private static IContainer container;
        static LogicService()
        {
            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterGeneric(typeof(ActionLogicImpl<>)).As(typeof(IActionLogicService<>)).SingleInstance();
            container = builder.Build();
        }

        public static IActionLogicService<T> IActionLogicService
        {
            get
            {
                return container.Resolve<IActionLogicService<T>>();
            }
        }
    }
}
